package com.txby.platformApi.component.web;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.jck.tools.MD5Util;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.pf.common.validator.group.UpdateGroup;
import com.txby.platformCore.entity.ProductDeviceInit;
import com.txby.platformCore.entity.ProductDeviceInitLot;
import com.txby.platformCore.entity.TxbySysApiUser;

/**
 * 产品信息初始化
 */
@RestController
@RequestMapping("/pro/deviceInit")
public class ProductDeviceInitController  extends CommonController {
	
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("pro:deviceInit:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="select t.sickAreaNO sickAreaNO,t.id_ id_,t.deviceSN deviceSN,t.bedNO bedNO,t.deptId deptId,t.updateTime ,t2.name deptName,t3.name sickAreaName  from  t_txby_productDeviceInit t,sys_dept t2,sys_dept t3 where t2.dept_id=t.deptId and t.sickAreaNO=t3.dept_id " ;
		if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and t.deptId in "+getJoinSqlBracket();
		}
		String bedNO=params.get("bedNO")==null?"":params.get("bedNO").toString();
		if(!ChkUtil.isEmptyAllObject(bedNO)){
			if (bedNO.contains("%") || bedNO.contains("_")) {
				bedNO = bedNO.trim().replace("%", "\\%").replace("_", "\\_");
				bedNO = "%" + bedNO + "%";
				sql += " and t.bedNO like #{pojo.bedNO} escape '\\'";
				params.put("bedNO", "%" + bedNO.trim() + "%");
			} else {
				bedNO = "%" + bedNO.trim() + "%";
				sql += " and t.bedNO like #{pojo.bedNO} ";
				params.put("bedNO", "%" + bedNO.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 查询信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("pro:deviceInit:info")
	public R info(@PathVariable("id") Long id){
		String sql="select t.sickAreaNO sickAreaNO,t.id_ id_,t.deviceSN deviceSN,t.bedNO bedNO,t.deptId deptId,t2.name deptName,t3.name sickAreaName from  t_txby_productDeviceInit t,sys_dept t2,sys_dept t3 where t2.dept_id=t.deptId and t.sickAreaNO=t3.dept_id and t.id_="+id+"" ;
		Map pro =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("pro", pro);
	}
	
	/**
	 * 保存信息
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@SysLog("保存初始化")
	@RequestMapping("/save")
	@RequiresPermissions("pro:deviceInit:save")
	public R save(@RequestBody ProductDeviceInit pd) throws IllegalAccessException, InvocationTargetException{
		ValidatorUtils.validateEntity(pd, AddGroup.class);
		String sql="select * from "+this.mBaseDao.tableName(ProductDeviceInit.class)+" where deviceSN='"+pd.getDeviceSN()+"'";
		ProductDeviceInit old=(ProductDeviceInit) this.mBaseDao.findBySqlUniqueObject(sql, ProductDeviceInit.class, null);
		if(old!=null){
	     old.setUpdater(getUserId().toString());
	     old.setUpdateTime(pd.getUpdateTime());
	     old.setBedNO(pd.getBedNO());
	     old.setSickAreaNO(pd.getSickAreaNO());
	     old.setDeptId(pd.getDeptId());
	     this.mBaseDao.updateObject(old);
	     ProductDeviceInitLot pdil=new ProductDeviceInitLot();
	     BeanUtils.copyProperties(pdil, old);
	     this.mBaseDao.updateObject(pdil);
		}else{
			sql="select * from "+this.mBaseDao.tableName(TxbySysApiUser.class)+"  where userName='"+pd.getDeviceSN()+"'";
//			db1e81c178f84c11f8dd19fa2b99c3f0
		/*	TxbySysApiUser  gau=(TxbySysApiUser) this.mBaseDao.findBySqlUniqueObject(sql, TxbySysApiUser.class, null);
			if(gau!=null) {
					gau.setPassWord(MD5Util.getMD5ofStr("db1e81c178f84c11f8dd19fa2b99c3f0", 2));
					this.mBaseDao.updateObject(gau);
					gau.setUpdateTime(new Date());
			}else{
					gau=new TxbySysApiUser();
					gau.setUserName(pd.getDeviceSN());
					gau.setPassWord(MD5Util.getMD5ofStr("db1e81c178f84c11f8dd19fa2b99c3f0", 2));
					this.mBaseDao.saveObject(gau);
				 
			}*/
			pd.setCreater(getUserId().toString());
			 pd.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(pd);
			 ProductDeviceInitLot pdil=new ProductDeviceInitLot();
		     BeanUtils.copyProperties(pdil, pd);
		     this.mBaseDao.saveObject(pd);
		}
		return R.ok();
	}
	
	/**
	 * 修改设备初始化
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@SysLog("修改设备初始化")
	@RequestMapping("/update")
	@RequiresPermissions("pro:deviceInit:update")
	public R update(@RequestBody ProductDeviceInit pd) throws IllegalAccessException, InvocationTargetException{
		ValidatorUtils.validateEntity(pd, UpdateGroup.class);
		String sql="select * from "+this.mBaseDao.tableName(ProductDeviceInit.class)+" where deviceSN='"+pd.getDeviceSN()+"'";
		ProductDeviceInit old=(ProductDeviceInit) this.mBaseDao.findBySqlUniqueObject(sql, ProductDeviceInit.class, null);
		if(old!=null){
	     old.setUpdater(getUserId().toString());
	     old.setUpdateTime(pd.getUpdateTime());
	     old.setBedNO(pd.getBedNO());
	     old.setSickAreaNO(pd.getSickAreaNO());
	     old.setDeptId(pd.getDeptId());
	     old.setSickAreaName(pd.getSickAreaName());
	     this.mBaseDao.updateObject(old);
		 ProductDeviceInitLot pdil=new ProductDeviceInitLot();
	     BeanUtils.copyProperties(pdil, old);
	     this.mBaseDao.updateObject(pdil);
		}else{
			pd.setCreater(getUserId().toString());
			 pd.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(pd);
			 ProductDeviceInitLot pdil=new ProductDeviceInitLot();
		     BeanUtils.copyProperties(pdil, pd);
		     this.mBaseDao.saveObject(pdil);
		}
		
		return R.ok();
	}
	
	/**
	 * 删除初始绑定设备
	 */
	@SysLog("删除初始绑定设备")
	@RequestMapping("/delete")
	@RequiresPermissions("pro:deviceInit:delete")
	public R delete(@RequestBody Long[] ids){
	    for(Long id:ids){
	    	this.mBaseDao.executeSql(" delete from t_txby_productDeviceInit where id_="+id+"", null);
	    }
		return R.ok();
	}
}
